﻿/*
创建人：zhangrj
日期：2023-02-28
描述：SPC预警维护页面js
*/
var page = {
    pageTotal: 0,
    pageSize: 10,
    pageNumber: 1,
    spcStepCheckItemDealID: "", //审核弹窗缓存处理记录ID
    ajaxUrl: getRootPath() + "/Ashx/StepCheckPointResultSPCHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        page.initData();
        customizedEasyuiPagination('pp'); //定制化分页
    },
    //页面事件绑定
    bind: function () {
        //列表加载
        $("#gdStepCheckPoint").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pageTotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'PRODUCTNAMEANDREV', title: '件号', width: 150 },
                 { field: 'WORKFLOWNAMEANDREV', title: '工艺', width: 180 },
                 { field: 'SPECNO', title: '工序号', width: 60 },
                 { field: 'OPDESC', title: '工序名', width: 100 },
                 { field: 'CONTAINERNAME', title: '批次', width: 120 },
                 { field: 'WORKERNAME', title: '工人', width: 60 },
                 { field: 'SPCRULE', title: '判异规则', width: 120,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             var rules = value.split(',');
                             var vals = "";
                             for (var i = 0; i < rules.length; i++) {
                                 if (rules[i] === "R0") {
                                     vals += "<a href='#' title='1个点超出上下限值（计数型）或与目标值不同（计量型）' class='easyui-tooltip'>" + rules[i] + "</a>&nbsp;&nbsp;&nbsp;";
                                 }
                                 else if (rules[i] === "R1") {
                                     vals += "<a href='#' title='连续9个点落在目标值的同一侧（上或下）' class='easyui-tooltip'>" + rules[i] + "</a>&nbsp;&nbsp;&nbsp;";
                                 }
                                 else if (rules[i] === "R2") {
                                     vals += "<a href='#' title='连续6个点递增或递减' class='easyui-tooltip'>" + rules[i] + "</a>&nbsp;&nbsp;&nbsp;";
                                 }
                                 else {
                                     vals += "<a href='#' title='连续14点在目标值上下交替' class='easyui-tooltip'>" + rules[i] + "</a>&nbsp;&nbsp;&nbsp;";
                                 }
                             }
                             return vals;
                         }
                     }
                 },
                 { field: 'SPCWARNNAME', title: '状态', width: 80 },
                 { field: 'LASTCHANGEDATE', title: '异常时间', width: 80,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD');
                         }
                     }
                 },
                 { field: 'DEALNAME', title: '处理人', width: 80 },
                 { field: 'DEALDATE', title: '处理时间', width: 80,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD');
                         }
                     }
                 },
                 { field: 'AUDITOR', title: '审核人', width: 80 },
                 { field: 'EXAMINEDATE', title: '审核时间', width: 80,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD');
                         }
                     }
                 }
                ]],
            onClickRow: function (rowIndex, rowData) {
                $("#dg").dialog("close");
            }
        });
        $("#gdReason").datagrid({
            columns: [[
                 { field: 'OUTCONTROL', title: '失控原因', width: 218, halign: 'center' },
                 { field: 'DEALMEASURE', title: '处理措施', width: 218, halign: 'center' }
                ]],
            onDblClickRow: function (rowIndex, rowData) {
                $("#txtWarnReason").textbox("setValue", rowData.OUTCONTROL);
                $("#txtSolution").textbox("setValue", rowData.DEALMEASURE);
            }
        });
        //查询按钮
        $('#btnSearch').on("click", function () {
            page.pageNumber = 1;
            page.search();
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.search();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.search();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.clearPage(true, true, true);
            page.pageNumber = 1; page.pageTotal = 0
            $('#pp').pagination('refresh', {
                total: page.pageTotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });
        //失控处理
        $('#btnDealWarning').on("click", function () {
            page.clearPage(false, false, true);
            page.initDialogData();
        });
        //存入历史经验
        $('#btnSaveHistory').on("click", function () {
            page.saveHistory();
        });
        //弹窗保存
        $("#btnSave").on("click", function () {
            page.saveWarningData();
        });
        //弹窗关闭
        $("#btnClose").on("click", function () {
            $("#dg").dialog("close");
        });
        //查看处理意见
        $("#btnViewIdea").on("click", function () {
            page.viewDeal();
        });
        //审核通过
        $("#btnPass").on("click", function () {
            page.saveDealResult(3);
        });
        //审核驳回
        $("#btnReject").on("click", function () {
            page.saveDealResult(1);
        });
    },
    //初始化页面数据
    initData: function () {
        page.post(page.ajaxUrl, { action: "InitSpcWarningData" }, false, function (res) {
            if (!res.Result) {
                page.disablePageBtn();
                displayMessage(res.Message, res.Result);
                return;
            }
            if (res.ReturnData === "1") { //如果是处理人，则隐藏审核按钮
                $("#btnDealdiv").hide();
            }
        });
    },
    //查询
    search: function () {
        var formData = {
            action: "GetSpcWarningData",
            pageSize: page.pageSize,
            pageIndex: page.pageNumber,
            productID: $("#productContent").productCombo("getValue"),
            workflowID: $("#workflowContent").workflowCombo("getValue"),
            specNo: $("#txtSpecNo").textbox("getValue"),
            warningTime: $("#txtWarningTime").datebox("getValue"),
            dealTime: $("#txtDealTimeByQuery").datebox("getValue"),
            examineTime: $("#txtExamineTime").datebox("getValue"),
            dealName: $("#txtDealName").textbox("getValue"),
            examineName: $("#txtAuditor").textbox("getValue")
        };
        page.post(page.ajaxUrl, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            page.pageTotal = res.RowCount;
            $("#gdStepCheckPoint").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //初始化弹窗页面并打开弹窗
    initDialogData: function () {
        var selected = $("#gdStepCheckPoint").datagrid("getChecked");
        if (selected.length === 0) {
            displayMessage("请选择需要处理的数据", false);
            return false;
        }
        var formData = {
            action: "InitSpcWarningDialogData",
            stepCheckResultID: selected[0].STEPCHECKPOINTRESULTID
        };
        page.post(page.ajaxUrl, formData, false, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            $("#gdReason").datagrid('loadData', { rows: res.DBTable });
            $("#processorName").html("处&nbsp理&nbsp人&nbsp：" + res.ReturnData);
            //打开弹窗
            $("#dg").dialog({
                onClose: function () { //关闭事件，恢复主页面按钮
                    page.enablePageBtn();
                }
            });
            page.disablePageBtn();
        });
    },
    //存入历史经验
    saveHistory: function () {
        $("#btnSaveHistory").linkbutton('disable');
        var warnReason = $("#txtWarnReason").textbox("getValue");
        var solution = $("#txtSolution").textbox("getValue");
        if (isNullOrWhiteSpaceStr(warnReason) && isNullOrWhiteSpaceStr(solution)) {
            $("#btnSaveHistory").linkbutton('enable');
            return;
        }
        var formData = {
            action: "SaveSpcHistoryExperienceData",
            warnReason: warnReason,
            solution: solution
        };
        page.post(page.ajaxUrl, formData, true, function (res) {
            $("#btnSaveHistory").linkbutton('enable');
            if (res.Result) {
                $('#gdReason').datagrid('insertRow', { index: 0, row: { OUTCONTROL: warnReason, DEALMEASURE: solution} });
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //保存失控处理
    saveWarningData: function () {
        $("#btnSave").linkbutton('disable');
        var selected = $("#gdStepCheckPoint").datagrid("getChecked");
        if (selected.length === 0) {
            displayMessage2("请选择需要处理的数据", false, ["btnSave"]);
            return false;
        }
        var warnReason = $("#txtWarnReason").textbox("getValue");
        var solution = $("#txtSolution").textbox("getValue");
        var dealTime = $("#txtDealTime").datetimebox("getValue");
        if (isNullOrWhiteSpaceStr(warnReason) && isNullOrWhiteSpaceStr(solution)) {
            displayMessage2("请填写完整数据", false, ["btnSave"]);
            return false;
        }
        var formData = {
            action: "SaveSpcStepCheckItemDeal",
            warnReason: warnReason,
            solution: solution,
            dealTime: dealTime,
            stepCheckResultID: selected[0].STEPCHECKPOINTRESULTID,
            spcStepCheckItemDealID: selected[0].SPCSTEPCHECKITEMDEALID
        };
        page.post(page.ajaxUrl, formData, true, function (res) {
            if (res.Result) {
                $("#dg").dialog("close");
                page.search();
            }
            displayMessage2(res.Message, res.Result, ["btnSave"]);
        });
    },
    //查看处理意见并打开弹窗
    viewDeal: function () {
        var selected = $("#gdStepCheckPoint").datagrid("getChecked");
        if (selected.length === 0) {
            displayMessage("请选择需要处理的数据", false);
            return false;
        }
        var formData = {
            action: "ViewSpcStepCheckItemDeal",
            stepCheckResultID: selected[0].STEPCHECKPOINTRESULTID
        };
        page.post(page.ajaxUrl, formData, false, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            if (res.ReturnData.IsShowDealBtn === "1") {
                $("#txtDealOpinion").textbox({ disabled: false });
                $("#btnDealdiv").show();
            } else {
                $("#btnDealdiv").hide();
                $("#txtDealOpinion").textbox({ disabled: true });
            }
            page.spcStepCheckItemDealID = res.ReturnData.SpcStepCheckItemDealID;
            $("#txtOutControl").textbox('setValue', res.ReturnData.OutControl);
            $("#txtDealMeasure").textbox('setValue', res.ReturnData.DealMeasure);
            $("#dealName").html("处理人：" + res.ReturnData.FullName);
            $("#dealTime2").html("处理时间：" + res.ReturnData.DealDate);
            $("#txtDealOpinion").textbox('setValue', res.ReturnData.DealOpinion);
            //打开弹窗
            $("#dg2").dialog({
                onClose: function () { //关闭事件，恢复主页面按钮
                    $("#mainDiv").find(".easyui-linkbutton").each(function () {
                        $(this).linkbutton('enable');
                    });
                }
            });
            //禁用主页面所有按钮
            $("#mainDiv").find(".easyui-linkbutton").each(function () {
                $(this).linkbutton('disable');
            });
        });
    },
    //保存审核结果
    saveDealResult: function (type) {
        $("#btnPass").linkbutton('disable');
        $("#btnReject").linkbutton('disable');
        if (isNullOrWhiteSpaceStr(page.spcStepCheckItemDealID)) {
            displayMessage2("缓存过期，请刷新页面", false, ["btnPass", "btnReject"]);
            return false;
        }
        var dealOpinion = $("#txtDealOpinion").textbox("getValue");
        if (isNullOrWhiteSpaceStr(dealOpinion)) {
            displayMessage2("请填写审核意见", false, ["btnPass", "btnReject"]);
            return false;
        }
        var formData = {
            action: "SaveSpcStepCheckDealResult",
            dealOpinion: dealOpinion,
            type: type,
            spcStepCheckItemDealID: page.spcStepCheckItemDealID
        };
        page.post(page.ajaxUrl, formData, true, function (res) {
            if (res.Result) {
                $("#dg2").dialog("close");
                page.search();
            }
            displayMessage2(res.Message, res.Result, ["btnPass", "btnReject"]);
        });
    },
    //禁用页面按钮
    disablePageBtn: function () {
        //禁用主页面所有按钮
        $("#mainDiv").find(".easyui-linkbutton").each(function () {
            $(this).linkbutton('disable');
        });
    },
    //恢复页面按钮
    enablePageBtn: function () {
        //恢复主页面所有按钮
        $("#mainDiv").find(".easyui-linkbutton").each(function () {
            $(this).linkbutton('enable');
        });
    },
    //清除页面（查询区域，列表区域，是否清空消息）
    clearPage: function (query, list, msg) {
        if (query) {
            $("#productContent").productCombo("init"); //件号
            $("#workflowContent").workflowCombo("init"); //工艺
            $("#txtSpecNo").textbox("setValue", ""); //流水卡
            $("#txtWarningTime").datebox("setValue", "");
            $("#txtDealTimeByQuery").datebox("setValue", "");
            $("#txtExamineTime").datebox("setValue", "");
        }
        if (list) {
            clearEasyuiDataGrid("gdContainerInfo"); //清空列表
        }
        if (msg) {
            clearMessage();
        }
        $("#txtWarnReason").textbox("setValue", "");
        $("#txtSolution").textbox("setValue", "");
        clearEasyuiDataGrid("gdReason");
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
};
//件号-工艺路线组件对象（不能页面加载后绑定，不然无法生效）
var productLinkWorkflow = {
    init: function () {
        //绑定件号选择组件
        $("#productContent").productCombo({
            search: function () { //查询事件
                clearEasyuiDataGrid("gdStepCheckPoint");
                $("#workflowContent").workflowCombo("init"); //同时恢复初始化工艺下拉组件
                page.clearPage();
            },
            cancel: function () { //件号取消点击事件
                clearEasyuiDataGrid("gdStepCheckPoint");
                page.clearPage();
                $("#workflowContent").workflowCombo("init"); //同时恢复初始化工艺下拉组件
            },
            selected: function (val, text) {//件号下拉选择事件
                clearEasyuiDataGrid("gdStepCheckPoint");
                $("#workflowContent").workflowCombo("init"); //同时恢复初始化工艺下拉组件
                $("#workflowContent").workflowCombo("setCombo", { productName: text }); //联动工艺下拉
                page.clearPage();
            }
        });
        //绑定工艺选择组件
        $("#workflowContent").workflowCombo({
            cancel: function () {//工艺取消点击事件
                clearEasyuiDataGrid("gdStepCheckPoint");
                page.clearPage();
            },
            selected: function (val, text) {//工艺下拉选择事件
                //                page.search(val);
            }
        });
    }
};
productLinkWorkflow.init();
$(function () {
    page.init();
});